package com.vcs.frm.web.school;

import java.util.Enumeration;

import javax.servlet.http.HttpSession;

import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/school")
public class SchoolController {

	@RequestMapping(method = RequestMethod.GET)
	public ModelAndView schoolHome(Model model, HttpSession session) {
		
		Authentication auth = SecurityContextHolder.getContext().getAuthentication();
		System.out.println(auth);
		System.out.println(session.getAttributeNames());
		Enumeration<String> attrs = (Enumeration<String>)session.getAttributeNames();
		while(attrs.hasMoreElements())
		{
			System.out.println("session attr : "+attrs.nextElement());
		}
		System.out.println(session.getAttribute("SPRING_SECURITY_CONTEXT"));
		SecurityContext sContext = (SecurityContext) session.getAttribute("SPRING_SECURITY_CONTEXT");
		
		if(sContext != null && sContext.getAuthentication() != null)
		{
			User user = (User) sContext.getAuthentication().getPrincipal();
			System.out.println(user.getUsername());
			session.setAttribute("loggedUser", user);
		}
		
		ModelAndView mav = new ModelAndView();
		mav.setViewName("schoolHome");
		return mav;
	}

}
